<!--
  * 台卡验证
-->
<template>
  <a-modal title="台卡验证" :width="600" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="formData" :rules="rules" layout="vertical">
      <a-row :gutter="16">
        <a-col :span="24">
          <a-form-item label="台卡二维码" name="code">
            <a-input v-model:value="formData.code" placeholder="请输入台卡二维码" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">验证</a-button>
        <slot></slot>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick, computed, watch } from 'vue'
  import _ from 'lodash'
  import { message } from 'ant-design-vue'
  import { SmartLoading } from '/@/components/framework/smart-loading'
  import { smartSentry } from '/@/lib/smart-sentry'
  import { codeApi } from '/@/api/business/b-merchant/code/code-api'
  import { useRoute } from 'vue-router'
  const route = useRoute()
  // ---------------------------- 表格列 ----------------------------

  const props = defineProps({
    open: {
      type: Boolean,
      default: false,
    },
    // 表单类型 add/update/view
    // add: 新增, update: 编辑,view: 查看
    formType: {
      type: String,
      default: 'add',
    },
    rowData: {
      type: Object,
      default: () => {},
    },
  })
  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['ok', 'update:open'])
  const visibleFlag = computed({ get: () => props.open, set: (val) => emits('update:open', val) })

  // ------------------------ 显示与隐藏 ------------------------
  function onClose() {
    Object.assign(formData.value, formDefault)
    visibleFlag.value = false
    nextTick(() => {
      formRef.value?.clearValidate()
    })
  }

  // ------------------------ 获取表单数据 ------------------------
  async function getFormData(data) {
    SmartLoading.show()
    try {
      let res = await codeApi.findId(data)
      formData.value = { ...res.data }
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }
  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref()

  const formDefault = {
    code: undefined,
  }

  let formData = ref({ ...formDefault })

  const rules = {
    code: [{ required: true, message: '开卡编码 必填' }],
  }

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields()
      save()
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!')
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show()
    try {
      let data = { ...formData.value } // ...props.rowData, deviceSN: props.rowData.sn
      await codeApi.verify(data)
      message.success('验证成功')
      await onPass()
      onClose()
    } catch (err) {
      smartSentry.captureError(err)
    } finally {
      SmartLoading.hide()
    }
  }

  async function onPass() {
    let data = { ...formData.value, verify: 1 } // ...props.rowData, deviceSN: props.rowData.sn
    await codeApi.verify(data)
    emits('ok')
  }

  watch(
    [() => props.rowData, () => props.open],
    (val) => {
      if (props.open && props.rowData && props.formType != 'add') {
        getFormData(val[0])
      }
    },
    { immediate: true },
  )
</script>
